package com.example.dl645.controller;

import com.example.dl645.model.MeterData;
import com.example.dl645.service.MeterReaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MeterController {

    @Autowired
    private MeterReaderService readerService;

    /**
     * 查询指定集中器下的电表数据
     * @param concentratorId 集中器ID（如"21180405"）
     * @param meterAddress 电表地址（12位十六进制）
     */
    @GetMapping("/api/meter/active-energy")
    public ResponseEntity<?> getActiveEnergy(
            @RequestParam String concentratorId,
            @RequestParam String meterAddress) {
        try {
            MeterData data = readerService.queryActiveEnergy(concentratorId, meterAddress);
            return ResponseEntity.ok(data);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
}